package com.gitee.xmhzzz.component.spring.cloud.component.dao.dto;



import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.xmhzzz.component.common.enums.Pager;

import java.util.stream.Collectors;



public class PagerAdapter<T> extends Page<T> {

    public static <T> PagerAdapter<T> adapt(Pager pager) {
        PagerAdapter<T> p = new PagerAdapter<>();
        p.setSize(pager.getRowsPerPage());
        p.setCurrent(pager.getCurrentPage());
        if(CollUtil.isNotEmpty(pager.getOrders())){
            p.setOrders(pager.getOrders().stream()
                .map(item -> (item.isAsc() ? OrderItem.asc(item.getColumn()) : OrderItem.desc(item.getColumn())))
                .collect(Collectors.toList()));
        }
        p.setTotal(pager.getTotalRow());
        return p;
    }
}
